<template>
  <el-card class="guest-container">
    <template #header>
      <div class="header">
        <el-button type="primary" size="small" icon="el-icon-plus" @click="handleAdd">新增用户</el-button>
        <el-popconfirm
            title="确定删除吗？"
            @confirm="handleDelete"
        >
          <template #reference>
            <el-button type="danger" size="small" icon="el-icon-delete">批量删除</el-button>
          </template>
        </el-popconfirm>
      </div>
    </template>
    <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          prop="id"
          label="用户id"
      >
      </el-table-column>
      <el-table-column
          prop="avatar"
          label="用户头像"
      >
        <template #default="scope">
          <img v-if="scope.row.avatar" :src="scope.row.avatar" style="height:40px;width: 40px;border-radius: 10px"
               class="avatar" alt=""/>
        </template>
      </el-table-column>
      <el-table-column
          prop="nickname"
          label="昵称"
      >
      </el-table-column>
      <el-table-column
          prop="username"
          label="登录名"
      >
      </el-table-column>
      <el-table-column
          prop="phoneNumber"
          label="手机号码"
      >
      </el-table-column>
      <el-table-column
          prop="mail"
          label="邮箱"
      >

      </el-table-column>

      <el-table-column
          prop="major"
          label="专业"
      >

      </el-table-column>

      <el-table-column
          prop="mail"
          label="角色"
      >
        <template #default="scope">
          <el-tag v-for="info in scope.row.roleIds">
            {{ roleOptions.get(info) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="createTime"
          label="注册时间"
          :formatter="dateFormat"
      >
      </el-table-column>
      <el-table-column
          prop="updateTime"
          label="更新时间"
          :formatter="dateFormat"
      >
      </el-table-column>
      <el-table-column
          label="操作"
      >
        <template #default="scope">
          <a style="cursor: pointer; margin-right: 10px" @click="handleEdit(scope.row.id)">修改</a>
          <el-popconfirm
              title="确定删除吗？"
              @confirm="handleDeleteOne(scope.row.id)"
          >
            <template #reference>
              <a style="cursor: pointer">删除</a>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--总数超过一页，再展示分页器-->
    <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :page-size="pageSize"
        :current-page="currentPage"
        @current-change="changePage"
    />
  </el-card>
  <DialogAddUser ref='addUser' :reload="getUserList" :type="type"/>
</template>

<script>
import {onMounted, reactive, ref, toRefs} from 'vue'
import axios from '@/utils/axios'
import {ElMessage} from 'element-plus'
import moment from "moment"
import DialogAddUser from "@/components/DialogAddUser.vue";

export default {
  name: 'Guest',
  components: {
    DialogAddUser
  },
  setup() {
    const multipleTable = ref(null)
    const addUser = ref(null)
    const state = reactive({
      loading: false,
      tableData: [], // 数据列表
      multipleSelection: [], // 选中项
      total: 0, // 总条数
      currentPage: 1, // 当前页
      pageSize: 10,// 分页大小
      type: 'add', // 操作类型
      roleOptions: new Map(),
    })
    onMounted(() => {
      getUserList()
      getRoleList()
    })
    // 获取轮播图列表
    const getUserList = () => {
      state.loading = true
      axios.get('/evaluate/user/page', {
        params: {
          current: state.currentPage,
          size: state.pageSize,
          name: ''
        }
      }).then(res => {
        state.tableData = res.records
        state.total = res.total
        state.currentPage = res.current
        state.loading = false
      })
    }
    // 选择项
    const handleSelectionChange = (val) => {
      state.multipleSelection = val
    }
    const changePage = (val) => {
      state.currentPage = val
      getUserList()
    }
    // 删除
    const handleDelete = () => {
      const userIds = state.multipleSelection.map(i => i.id);
      axios.delete("/evaluate/user", {data: userIds}).then(() => {
        ElMessage.success('删除成功')
        getUserList()
      })
    }
    //添加
    const handleAdd = (id) => {
      state.type = 'add'
      addUser.value.open()
    }
    // 修改分类
    const handleEdit = (id) => {
      state.type = 'edit'
      addUser.value.open(id)
    }
    // 删除一个
    const handleDeleteOne = (id) => {
      axios.delete("/evaluate/user", {data: [id]}).then(() => {
        ElMessage.success("删除成功")
        getUserList()
      })
    }

    // 获取角色列表
    const getRoleList = () => {
      axios.get("/evaluate/role").then(res => {
        res.map(role => state.roleOptions.set(role.id, role.name))
      })
    }
    return {
      ...toRefs(state),
      multipleTable,
      addUser,
      handleSelectionChange,
      getUserList,
      changePage,
      handleAdd,
      handleDelete,
      handleDeleteOne,
      handleEdit,
      dateFormat: function (row, column) {
        const date = row[column.property];
        if (date === undefined) {
          return ''
        }
        return moment(date).format("YYYY-MM-DD HH:mm:ss")
      }
    }
  }
}
</script>

<style scoped>
.guest-container {
  min-height: 100%;
}

.el-card.is-always-shadow {
  min-height: 100% !important;
}
</style>